//
//  MBProgressHUD+HUD.m
//  Goin
//
//  Created by Flyfreely on 2018/8/17.
//  Copyright © 2018年 Goin. All rights reserved.
//

#import "MBProgressHUD+HUD.h"

@implementation MBProgressHUD (HUD)

#pragma mark 显示错误信息
+ (void)showError:(NSString *)error ToView:(UIView *)view{
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self showCustomIcon:@"neironglam_tishi_caozuoshibai.png" Title:error ToView:view];
    });
    
}

#pragma mark 显示成功信息
+ (void)showSuccess:(NSString *)success ToView:(UIView *)view
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self showCustomIcon:@"neironglan_tishi_caozuochenggong.png" Title:success ToView:view];
    });
    
}

#pragma mark 自定义一个带图片的HUD
+ (void)showCustomIcon:(NSString *)iconName Title:(NSString *)title ToView:(UIView *)view
{
    if (view == nil) view = (UIView*)[UIApplication sharedApplication].delegate.window;
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    //    hud.labelText = title;
    hud.label.text = title;
    hud.label.font = [UIFont systemFontOfSize:15];
    hud.bezelView.color = [[UIColor whiteColor]colorWithAlphaComponent:1.0];
    hud.contentColor = UIColorHex(666666);
    
    // 设置图片
    if ([iconName isEqualToString:@"error.png"] || [iconName isEqualToString:@"success.png"]) {
        hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"MBProgressHUD.bundle/%@", iconName]]];
    }else{
        hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:iconName]];
    }
    // 再设置模式
    hud.mode = MBProgressHUDModeCustomView;
    
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    
    // 1.5秒之后再消失
    [hud hideAnimated:YES afterDelay:1.5];
}

#pragma mark 显示一些信息(菊花+文字)
+ (MBProgressHUD *)showMessage:(NSString *)message ToView:(UIView *)view {
    
    __block MBProgressHUD *hud;
    __block UIView *superView = view;
    dispatch_async(dispatch_get_main_queue(), ^{
        // 快速显示一个提示信息
        if (superView == nil) superView = (UIView*)[UIApplication sharedApplication].delegate.window;
        hud = [MBProgressHUD showHUDAddedTo:superView animated:YES];
        hud.label.font = [UIFont systemFontOfSize:15];
        hud.label.text = message;
        hud.bezelView.color = [[UIColor blackColor]colorWithAlphaComponent:0.8];
        hud.contentColor = [UIColor whiteColor];
        // 隐藏时候从父控件中移除
        hud.removeFromSuperViewOnHide = YES;
    });

    return hud;
}

//自动消失提示，无图
+ (void)showAutoMessage:(NSString *)message ToView:(UIView *)view{
    
    dispatch_async(dispatch_get_main_queue(), ^{
       [self showMessage:message ToView:view RemainTime:1.0 Model:MBProgressHUDModeText];
    });
    
}

//自定义停留时间，无图
+(void)showMessage:(NSString *)message ToView:(UIView *)view RemainTime:(CGFloat)time{
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self showMessage:message ToView:view RemainTime:time Model:MBProgressHUDModeText];
    });
    
}

//快速创建一个HUD
+(void)showMessage:(NSString *)message ToView:(UIView *)view RemainTime:(CGFloat)time Model:(MBProgressHUDMode)model{
    
    if (view == nil) view = (UIView*)[UIApplication sharedApplication].delegate.window;
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.label.font = [UIFont systemFontOfSize:15];
    hud.label.text = message;
    hud.bezelView.color = [[UIColor blackColor]colorWithAlphaComponent:0.8];
    hud.contentColor = [UIColor whiteColor];

    //模式
    hud.mode = model;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    // YES代表需要蒙版效果
    //    hud.dimBackground = YES;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    // X秒之后再消失
    //    [hud hide:YES afterDelay:time];
    
    [hud hideAnimated:YES afterDelay:time];
    
}

+ (void)hideHUDForView:(UIView *)view
{
    __block UIView *superView = view;
    dispatch_async(dispatch_get_main_queue(), ^{
        if (superView == nil) superView = (UIView*)[UIApplication sharedApplication].delegate.window;
        [self hideHUDForView:superView animated:YES];
    });
    
}

@end
